package com.example.prop_logic;

import java.util.ArrayList;

public class PropLogicParser {

    private final ArrayList<Character> propList = new ArrayList<Character>();
    private final String fullExpression;
    private final ArrayList<Character> propInExpression = new ArrayList<Character>();

    PropLogicParser(String expression) {
        this.fullExpression = expression;
        addStringToArrayList(this.propList, 'P', 'Q', 'R', 'W', 'A', 'B', 'C',
                'D', 'E', 'F');
        findPropInExpression(this.propInExpression, this.propList,
                this.fullExpression);
    }

    /**
     * Finds instances of elements in find in expression and if it exists and is
     * not in found already, put it in found.
     * 
     * @param found
     *            The ArrayList which stores the elements which are both in find
     *            and expression.
     * @param find
     *            The ArrayList which has the elements we are trying to find.
     * @param expression
     *            The expression which may contain elements in find.
     * 
     * @replaces found
     * @requires find contains no empty strings or strings which are substrings
     *           of other elements of find.
     * @ensures found contains each element of find which is included in
     *          expression and each element of found is unique.
     */
    private static void findPropInExpression(ArrayList<Character> found,
            ArrayList<Character> find, String expression) {
        found.clear();

        int eLength = expression.length();

        int index = 0;

        while (index < eLength) {
            Character c = expression.charAt(index);

            if (find.contains(c) && !found.contains(c)) {
                found.add(c);
            }

            index++;
        }
    }

    /**
     * Adds given args to a.
     * 
     * @param a
     *            The ArrayList which we are filling with args.
     * @param args
     *            The args which we are putting in a.
     * 
     * @updates a
     * @requires Each element of args is unique.
     * @ensures Each args is added to a.
     */
    private static void addStringToArrayList(ArrayList<Character> a,
            Character... args) {
        for (Character c : args) {
            a.add(c);
        }
    }

    public ArrayList<Character> getPropInExpression() {
        return this.propInExpression;
    }

    public String getExpression() {
        return this.fullExpression;
    }

}
